package com.cfw.util;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;


public class GridUtil {

	public static final int DEFAULT_PAGE = 1;
	public static final int DEFAULT_ROWS = 20;
	
	public static final int DEF_GRID_COL_IDX = 0;
	public static final int DEF_TABLE_COL_IDX = 1;
	public static final int DEF_TAG_COL_IDX = 2;
	public static final String DEF_KEY_TOKEN = "key";
	public static final String DEF_COUNT_COL_NM = "COUNT";
	public static final String DEF_FIX_COUNT_COL_NM = "FIX_COUNT";
	
	public static class PAR_NM {
		public static final String SEARCH_VAL = "searchVal"; 
		public static final String SEARCH_COL = "searchCol";
		public static final String ORDER_COL = "sidx";
		public static final String ORDER_TYPE = "sord";
		public static final String ROWS = "rows";
		public static final String PAGE = "page";
		public static final String START = "start";
		public static final String END = "end";
		
		public static final String TOTAL = "total";
		public static final String RECORDS = "records";
		public static final String ID = "id";
		public static final String CELL = "cell";
	}
	
	public static void setupSearchParam(Map<String, Object> param, String[][] colDef) {
		if (colDef != null && param != null) {
			String searchCol = (String)param.get(PAR_NM.SEARCH_COL);
			param.remove(PAR_NM.SEARCH_COL);
					
			for (String[] def : colDef) {
				if (def[DEF_GRID_COL_IDX].equals(searchCol)) {
					param.put(PAR_NM.SEARCH_COL, def[DEF_TABLE_COL_IDX]);
					break;
				}
			}
		}			
	}
	
	public static void setupParam(Map<String, Object> param, String[][] colDef, Map<String, Object> counts) {
		long page = DEFAULT_PAGE;
		String pageStr = (String)param.get(PAR_NM.PAGE);
		if (StringUtils.isNotEmpty(pageStr)) {
			page = Long.parseLong(pageStr);
		}
		
		long rows = DEFAULT_ROWS;
		String rowsStr = (String)param.get(PAR_NM.ROWS);
		if (StringUtils.isNotEmpty(rowsStr)) {
			rows = Integer.parseInt(rowsStr); 
		}

		long count = ((BigDecimal)counts.get(DEF_COUNT_COL_NM)).longValue();
		long fixCount = ((BigDecimal)counts.get(DEF_FIX_COUNT_COL_NM)).longValue();
		
		long realRows = rows - fixCount; // 페이지당 행수에서 고정게시물을 제외한 페이지당 행수 

		long start;
		long end;
		long total;
		if (realRows <= 0) { // 고정게시물 수가 페이지당 표시 행수보다 클 경우 
			start = 0;
			end = 0;
			total = 1;
		} else {
			start = ((page - 1) * realRows) + 1;
			end = (start + realRows - 1);
			total = (int)Math.ceil((double)(count - fixCount) / (double)realRows);
		}

		param.put(PAR_NM.PAGE, page);
		param.put(PAR_NM.ROWS, rows);
		param.put(PAR_NM.START, start);
		param.put(PAR_NM.END, end);
		param.put(PAR_NM.TOTAL, total);
		param.put(PAR_NM.RECORDS, count);
	}
	
	public static Map<String, Object> getGridResultMap(Map<String, Object> param, List<Map<String, Object>> list, String[][] colDef) {
		Map<String, Object> result = new HashMap<String, Object>();
		
		// { 
		// 	"total": "xxx", // total pages for the query
		// 	"page": "yyy", // current page of the query
		// 	"records": "zzz", // total number of records for the query
		// 	"rows" : [ // an array that contains the actual data
		// 		{"id" :"1", "cell" :["cell11", "cell12", "cell13"]}, // id: the unique id of the row, cell: an array that contains the data for a row
		// 		{"id" :"2", "cell":["cell21", "cell22", "cell23"]},
		// 		...
		// 	]
		// }
		
		result.put(PAR_NM.PAGE, param.get(PAR_NM.PAGE));
		result.put(PAR_NM.TOTAL, param.get(PAR_NM.TOTAL));
		result.put(PAR_NM.RECORDS, param.get(PAR_NM.RECORDS));
		
		List<Map<String, Object>> rows = new ArrayList<Map<String, Object>>();
		if (list != null) {
			for (Map<String, Object> map : list) {
				Map<String, Object> row = new HashMap<String, Object>();
				List<Object> cell = new ArrayList<Object>();
				for (String[] col : colDef) {
					cell.add(map.get(col[DEF_TABLE_COL_IDX]));
					if (col.length >= 3 && DEF_KEY_TOKEN.equals(col[DEF_TAG_COL_IDX]))  {
						row.put(PAR_NM.ID, map.get(col[DEF_TABLE_COL_IDX]));
					}
				}
				row.put(PAR_NM.CELL, cell);
				rows.add(row);
			}
		}
		result.put(PAR_NM.ROWS, rows);
		
		return result;
	}
	
}
